//
//  SetViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/23.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "SetViewController.h"

#import "PersonInfoViewController.h"
#import "AboutYDDViewController.h"
#import "KFuViewController.h"
#import "AppDelegate.h"
@interface SetViewController ()<UITableViewDataSource,UITableViewDelegate>{
    NSArray *array1;
}

@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic, strong) NSString *fileCachePath;

@end

@implementation SetViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    self.view.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [self showNav:@"设置"];
    array1 = @[@"关于丫丢丢",@"给我们评分"];
    NSDictionary *dic = [user dictionaryRepresentation];
    NSLog(@"dic:%@",dic);
    
    [self loadUI];
    
    
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 44*6+44) style:UITableViewStylePlain];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.scrollEnabled = NO;
    _tableView.showsVerticalScrollIndicator = NO;//隐藏滚动条
    [self.view addSubview:_tableView];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, _tableView.frame.origin.y+_tableView.frame.size.height+20, ScreenWidth, 45);
    btn.backgroundColor = [UIColor whiteColor];
    [btn setTitle:@"退出登录" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(clickEixt) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
}
//退出登录
- (void)clickEixt{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"您真的要退出吗？" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
        //NSDictionary *dic = [user dictionaryRepresentation];
        [user removeObjectForKey:@"userid"];
        [user synchronize];
        AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
        UITabBarController *vc = delegate.tabbar;
        vc.selectedIndex = 0;
        [self.navigationController popViewControllerAnimated:YES];
        /*
        for (NSString *key in [dic allKeys]) {
            //移除user里面所有数据
            [user removeObjectForKey:key];
            [user synchronize];
        }*/
        
    }];
    
    [alert addAction:action1];
    [alert addAction:action2];
    [self presentViewController:alert animated:YES completion:nil];
    
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 5;
}
#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 4) {
        return 2;
    }
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellId];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.textLabel.font = [UIFont systemFontOfSize:15.0];
    cell.textLabel.textColor = [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0];
    
    if (indexPath.section == 0) {
        cell.textLabel.text = @"个人资料";
        
    }else if (indexPath.section == 1){
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.textLabel.text = @"语音提示";
    
        
        
    }else if (indexPath.section == 2){
        cell.textLabel.text = @"清除缓存";
        cell.detailTextLabel.text = [NSString stringWithFormat:@"%.1fM",[self getCacheSize]];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:14.0];
        
    }else if (indexPath.section == 3){
        cell.textLabel.text = @"客服中心";
    }else{
        cell.textLabel.text = array1[indexPath.row];
        
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    return 10.0;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        PersonInfoViewController *person = [[PersonInfoViewController alloc] init];
        person.imageUrl = _imageUrl;
        person.pickName = _pickName;
        person.phone = self.phone;
        [self.navigationController pushViewController:person animated:YES];
    }else if (indexPath.section == 1){
        
        
    }else if (indexPath.section == 2){
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"清除缓存" message:@"是否确认清理缓存" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
        
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            [self cleanCache];
            [self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
        }];
        
        [alert addAction:action1];
        [alert addAction:action2];
        [self presentViewController:alert animated:YES completion:nil];
    }else if (indexPath.section == 3){
        KFuViewController *kefu = [[KFuViewController alloc] init];
        [self.navigationController pushViewController:kefu animated:YES];
    }else{
        if (indexPath.row == 0) {
            AboutYDDViewController *ydd = [[AboutYDDViewController alloc] init];
            [self.navigationController pushViewController:ydd animated:YES];
        }else{
            NSString *str = @"https://itunes.apple.com/us/app/丫丢丢-一款生活服务类的app/id1288033798?l=zh&ls=1&mt=8";
            [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
        }
    }
}


-(CGFloat)getCacheSize {
    
    CGFloat fileTotalSize = 0.0;
    
    fileTotalSize = [[SDImageCache sharedImageCache] getSize];
    
    return fileTotalSize/1024.0/1024.0;
    
    
}

//清除缓存
-(void) cleanCache
{
    [[SDImageCache sharedImageCache] clearDiskOnCompletion:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
